<?php

class Adept_Binding_Parser 
{
    
    static function parseValueBinding($string, $binding) 
    {
        if (preg_match('~\#(\w+)(?:\[([^\]]+)\])?(?:\-\>(\w+)(?:\[([^\]]+)\])?)?~', $string, $match)) {
            $objectId = $match[1];
            $objectIndex = !empty($match[2]) ? $match[2] : null;        
            $property = !empty($match[3]) ? $match[3] : null;
            $propertyIndex = !empty($match[4]) ? $match[4] : null;
        } else {
            throw new Adept_Exception("Invalid value binding syntax: '{$string}' ");
        }
        
        $binding->setObjectId($objectId);
        
        if (!is_null($objectIndex)) {
            $binding->setObjectIndex($objectIndex);
        }
    
        if (!is_null($property)) {
            $binding->setProperty($property);    
        }
        if (!is_null($propertyIndex)) {
            $binding->setPropertyIndex($propertyIndex);
        }
    }
    
    static function parseMethodBinding($string, $binding) 
    {
        if (preg_match('~\#(\w+)(?:\[([^\]]+)\])?\-\>(\w+)~', $string, $match)) {
            $objectId = $match[1];
            $objectIndex = !empty($match[2]) ? $match[2] : null;
            $method = $match[3];
        } else {
            throw new Adept_Exception("Invalid method binding syntax: '{$string}' ");
        }
    
        $binding->setObjectId($objectId);
        if (!is_null($objectIndex)) {
             $binding->setObjectIndex($objectIndex);
        }
        $binding->setMethod($method);
    }
    
} 